package com.aijk.ehm.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.dao.mybatis.EhmInfoScanMapper;
import com.aijk.ehm.model.EhmInfoScan;
import com.aijk.ehm.model.EhmInfoScanExample;
import com.aijk.ehm.model.qo.EhmInfoScanQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EhmInfoScanService {
    private static Logger logger = LoggerFactory.getLogger(EhmInfoScanService.class);

    @Autowired
    private EhmInfoScanMapper mapper;

    public EhmInfoScanExample getExample(EhmInfoScanQo qo) {
        EhmInfoScanExample example = new EhmInfoScanExample();
        EhmInfoScanExample.Criteria criteria = example.createCriteria();
        if (qo != null) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (qo.getStartTime()!= null) criteria.andScanTimeGreaterThanOrEqualTo(qo.getStartTime());
            if (qo.getEndTime()!= null) criteria.andScanTimeLessThanOrEqualTo(qo.getEndTime());
            if (qo.getNewsid()!= null) criteria.andNewsidEqualTo(qo.getNewsid());
        }
        return example;
    }

    public int count(EhmInfoScanQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public int save(EhmInfoScan bean) {
        return mapper.insertSelective(bean);
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int update(EhmInfoScan bean) {
        return mapper.updateByPrimaryKeySelective(bean);
    }

    public EhmInfoScan findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<EhmInfoScan> find(EhmInfoScanQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<EhmInfoScan> page(PagingRequest pagingRequest, EhmInfoScanQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum() , pagingRequest.getPageSize());
        return new PageInfo<EhmInfoScan>(mapper.selectByExample(getExample(qo)));
    }
}